package com.android.clinic.app.ui.pay;

import android.content.Context;

import com.android.clinic.app.utils.Toasty;
import com.android.clinic.app.utils.Utils;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONObject;

/**
 * Created by cheng on 2018/3/9.
 */
public class ClinicWeChatPay {

    private Context activity;
    private JSONObject json;

    public ClinicWeChatPay(Context activity, JSONObject jsonObject) {
        this.activity = activity;
        this.json = jsonObject;
    }

    public void initPay() {
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(activity, null);
        if (!iwxapi.isWXAppInstalled()) {
            Toasty.showShort(activity, "您还未安装微信客户端!");
            return;
        } else if (null == this.json) {
            Toasty.showShort(activity, "支付信息有误!");
        } else if (Utils.isEmpty(this.json.optString("appid"))) {
            Toasty.showShort(activity, "微信参数有误!");
        }
        iwxapi.registerApp(this.json.optString("appid"));
        PayReq req = new PayReq();
        req.appId = json.optString("appid");
        req.nonceStr = json.optString("noncestr");
        req.packageValue = json.optString("package");
        req.partnerId = json.optString("partnerid");
        req.prepayId = json.optString("prepayid");
        req.timeStamp = json.optString("timestamp");
        req.sign = json.optString("sign");
        iwxapi.sendReq(req);
    }

}
